package com.example.springboot.wechat.wechatinterface.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Map;

/**
 * 获取微信公众号信息
 */
public class WeChatUtils {

	private static final Log log = LogFactory.getLog(WeChatUtils.class);

	/**
	 * 获取全局token缓存
	 * grant_type	是	获取access_token填写client_credential
	 * appid	是	第三方用户唯一凭证
	 * secret	是	第三方用户唯一凭证密钥，即appsecret
	 * 返回结果：
	 * {"access_token":"ACCESS_TOKEN","expires_in":7200}
	 * access_token	获取到的凭证，默认2小时有效
	 * expires_in	凭证有效时间，单位：秒
	 */
	public static Map<String, String> getAccessToken(String appid, String appsecret) throws Exception {
		// 加载全局token缓存
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
		String res = HttpClientUtils.get(url);
		log.info("getAccessToken-->获取全局token缓存结果：" + res.toString());
		Map<String, String> map = GsonUtils.fromJson(res, Map.class);
		return map;
	}

	/**
	 * 获取web端js-sdk token缓存
	 * access_token： 全局缓存access_token
	 * 返回结果：
	 * {"errcode":0,
	 * "errmsg":"ok",
	 * "ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
	 * "expires_in":7200 }
	 */
	public static Map<String, String> getJSSDKToken(String access_token) throws Exception {
		String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + access_token + "&type=jsapi";
		String res = HttpClientUtils.get(url);
		log.info("getJSSDKToken-->获取web端js-sdk token缓存结果：" + res.toString());
		Map<String, String> map = GsonUtils.fromJson(res, Map.class);
		return map;
	}

	/**
	 * 获取web端网页token
	 * appid	是	公众号的唯一标识
	 * secret	是	公众号的appsecret
	 * code	是	填写第一步获取的code参数
	 * grant_type	是	填写为authorization_code
	 * <p>
	 * 返回结果：
	 * { "access_token":"ACCESS_TOKEN",
	 * "expires_in":7200,
	 * "refresh_token":"REFRESH_TOKEN",
	 * "openid":"OPENID",
	 * "scope":"SCOPE" }
	 * access_token	网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同，默认2小时有效
	 * expires_in	access_token接口调用凭证超时时间，单位（秒）
	 * refresh_token	用户刷新access_token
	 * openid	用户唯一标识，请注意，在未关注公众号时，用户访问公众号的网页，也会产生一个用户和公众号唯一的OpenID
	 * scope	用户授权的作用域，使用逗号（,）分隔
	 */
	public static Map<String, String> getWebAccessToken(String appid, String appsecret, String code) throws Exception {
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";
		String res = HttpClientUtils.get(url);
		Map<String, String> map = GsonUtils.fromJson(res, Map.class);
		log.info("getWebAccessToken-->获取web端网页token结果：" + map.toString());
		return map;
	}

	/**
	 * 根据重定向url查询Code
	 *
	 * @param redirect_uri
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getAuthorizeCode(String redirect_uri, String openid) throws Exception {
		String url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + openid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
		String res = HttpClientUtils.get(url);
		Map<String, Object> map = GsonUtils.fromJson(res, Map.class);
		log.info("getCode-->根据重定向url查询Code：" + map.toString());
		return map;
	}

	/**
	 * 根据code查询openid
	 *
	 * @param code
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> getOpenID(String code, String openid, String appsecret) throws Exception {
		String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + openid + "&secret=" + appsecret + "&code=" + code + "&grant_type=authorization_code";
		String res = HttpClientUtils.get(url);
		log.info("根据code查询openid结果： " + res.toString());
		Map<String, Object> map = GsonUtils.fromJson(res, Map.class);
		log.info("getOpenID-->根据code查询openid：" + map.toString());
		return map;
	}

	/**
	 * 获取用户信息
	 * access_token	网页授权接口调用凭证,注意：此access_token与基础支持的access_token不同，默认2小时有效
	 * openid	用户的唯一标识
	 * lang	返回国家地区语言版本，zh_CN 简体，zh_TW 繁体，en 英语
	 */
	public static Map<String, String> getUserInfo(String access_token, String openid) throws Exception {
		String url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
		String res = HttpClientUtils.get(url);
		log.info("getUserInfo-->获取用户信息结果：" + res.toString());
		Map<String, String> map = GsonUtils.fromJson(res, Map.class);
		return map;
	}
}
